package cc.autoapi.pucong.auto.flow.persistence.repository.api;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.script.DefaultAiScript;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.send.AbstractAiSendSendTool;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.text.DefaultAiText;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.api.design.ApiNodeParamsVo;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.api.design.ApiNodeRelationVo;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.api.design.ApiNodeVo;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.*;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.DataTypeEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.ParamPositionEnum;
import cc.autoapi.pucong.auto.flow.core.root.properties.AutoFlowDesignConfiguration;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author pucong
 * @description api 关联节点信息 service 实现类
 * @create 2024-09-24
 */
@Service
public class ApiNodeServiceImpl implements ApiNodeService {

    @Resource
    private AutoFlowDesignConfiguration autoFlowDesignConfiguration;

    @Resource
    private AbstractAiSendSendTool aiSendSendTool;

    @Override
    public String getScriptByAi(String nodeName, String desc, String nodeType, List<AiNodeScriptParamIn> ins, List<AiNodeScriptParamOut> outs) {
        DefaultAiScript defaultAiScript = new DefaultAiScript();
        defaultAiScript.initAiTool(aiSendSendTool);
        String s = defaultAiScript.getScript(nodeName, desc, ins, outs, nodeType);
        return s;
    }


    private List<TaskNodeParamOut> getParamsOuts(String nodeId, List<ApiNodeParamsVo> apiNodeParamsPos) {
        List<ApiNodeParamsVo> collect = apiNodeParamsPos.stream().filter(item -> item.getApiNodeId().equals(nodeId) && item.getParamType() == 1).collect(Collectors.toList());
        List<TaskNodeParamOut> taskNodeParamOutList = new ArrayList<>();
        for (ApiNodeParamsVo apiNodeParamsPo : collect) {
            TaskNodeParamOut taskNodeParamOut = new TaskNodeParamOut();
            taskNodeParamOut.setUuid(apiNodeParamsPo.getApiNodeId() + "_out_" + apiNodeParamsPo.getParamCode());
            taskNodeParamOut.setApiNodeId(apiNodeParamsPo.getApiNodeId());
            taskNodeParamOut.setParamCode(apiNodeParamsPo.getParamCode());
            taskNodeParamOut.setDateType(DataTypeEnum.getItemByValue(apiNodeParamsPo.getDateType()));
            taskNodeParamOut.setReturnValue(apiNodeParamsPo.getReturnValue());
            taskNodeParamOut.setIsBreakParams(apiNodeParamsPo.getIsBreakParams());
            taskNodeParamOutList.add(taskNodeParamOut);
        }
        return taskNodeParamOutList;
    }

    private List<TaskNodeParamIn> getParamsIns(String nodeId, List<ApiNodeParamsVo> apiNodeParamsPos) {
        List<ApiNodeParamsVo> collect = apiNodeParamsPos.stream().filter(item -> item.getApiNodeId().equals(nodeId) && item.getParamType() == 0).collect(Collectors.toList());
        List<TaskNodeParamIn> taskNodeParamInList = new ArrayList<>();
        for (ApiNodeParamsVo apiNodeParamsPo : collect) {
            TaskNodeParamIn taskNodeParamIn = new TaskNodeParamIn();
            taskNodeParamIn.setUuid(apiNodeParamsPo.getApiNodeId() + "_in_" + apiNodeParamsPo.getParamCode());
            taskNodeParamIn.setApiNodeId(apiNodeParamsPo.getApiNodeId());
            taskNodeParamIn.setParamCode(apiNodeParamsPo.getParamCode());
            taskNodeParamIn.setParamValue(apiNodeParamsPo.getParamValue());
            taskNodeParamIn.setDateType(DataTypeEnum.getItemByValue(apiNodeParamsPo.getDateType()));
            taskNodeParamIn.setNeedValue(apiNodeParamsPo.getNeedValue());
            taskNodeParamIn.setParamPosition(ParamPositionEnum.getItemByValue(apiNodeParamsPo.getParamPosition()));
            taskNodeParamIn.setParamDataType(apiNodeParamsPo.getParamDataType());
            taskNodeParamInList.add(taskNodeParamIn);
        }
        return taskNodeParamInList;
    }

    @Override
    public List<TaskNode> getTaskNodeList(List<ApiNodeVo> addNodeList, List<ApiNodeParamsVo> apiNodeParamsVoList) {
        List<TaskNode> execNodeList = new ArrayList<>();
        for (ApiNodeVo apiNodePo : addNodeList) {
            TaskNode execNode = new TaskNode();
            execNode.setUuid(apiNodePo.getUuid());
            execNode.setNodeName(apiNodePo.getNodeName());
            execNode.setNodeTag(apiNodePo.getNodeTag());
            execNode.setNodeType(apiNodePo.getNodeType());
            execNode.setNodeScript(apiNodePo.getNodeScript());
            execNode.setProcessId(apiNodePo.getProcessId());
            execNode.setForAsync(apiNodePo.getForAsync());
            execNode.setForValue(apiNodePo.getForValue());
            execNode.setForDataType(apiNodePo.getForDataType());

            // 输入参数
            List<TaskNodeParamIn> taskNodeParamInList = getParamsIns(apiNodePo.getUuid(), apiNodeParamsVoList);
            // 输出参数
            List<TaskNodeParamOut> taskNodeParamOutList = getParamsOuts(apiNodePo.getUuid(), apiNodeParamsVoList);

            execNode.setTaskNodeParamInList(taskNodeParamInList);
            execNode.setTaskNodeParamOutList(taskNodeParamOutList);
            execNodeList.add(execNode);
        }
        return execNodeList;
    }

    @Override
    public List<TaskNodeRelation> getTaskNodeRelationList(List<ApiNodeRelationVo> addRelateList) {
        List<TaskNodeRelation> list = new ArrayList<>();
        for (ApiNodeRelationVo apiNodeRelationPo : addRelateList) {
            list.add(new TaskNodeRelation(apiNodeRelationPo.getRelationType(), apiNodeRelationPo.getNodeFromId(), apiNodeRelationPo.getNodeToId()));
        }
        return list;
    }

    @Override
    public String getTextByAi(String desc, String content) {
        DefaultAiText defaultAiText = new DefaultAiText();
        defaultAiText.initAiTool(aiSendSendTool);
        String s = defaultAiText.getText(desc, content);
        return s;
    }
}
